home *** CD-ROM | disk | FTP | other *** search
- unit UCAniTrayIcon;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, UCAniIcon;
-
- const
- UCTrayCallBack = $89AB;
-
- type
- TUCAniTrayIcon = class (TComponent)
- private
- { Private declarations }
- fHint: String;
- fWindowHandle: hWnd;
- fVisible: Boolean;
- fAniIcon: TAniIcon;
- fOnLeftClick: TNotifyEvent;
- fOnRightClick: TNotifyEvent;
- fExclusionLock: Boolean;
- fAnimate: Boolean;
- procedure SetHint (const Value: String);
- procedure SetVisible (Value: Boolean);
- procedure SetAniIcon (Value: TAniIcon);
- procedure UpdateTray (Visible: Boolean);
- procedure SetAnimate (Value: Boolean);
- procedure WndProc (var Message: TMessage);
- procedure TrayMessage (var Message: TMessage);
- protected
- { Protected declarations }
- procedure Loaded; override;
- public
- { Public declarations }
- constructor Create (AOwner: TComponent); override;
- destructor Destroy; override;
- published
- { Published declarations }
- property Icon: TAniIcon read fAniIcon write SetAniIcon;
- property Hint: String read fHint write SetHint;
- property Animate: Boolean read fAnimate write SetAnimate default False;
- property Visible: Boolean read fVisible write SetVisible default False;
- property OnLeftClick: TNotifyEvent read fOnLeftClick write fOnLeftClick;
- property OnRightClick: TNotifyEvent read fOnRightClick write fOnRightClick;
- end;
-
- procedure Register;
-
- implementation
-
- uses ShellAPI;
-
- constructor TUCAniTrayIcon.Create (AOwner: TComponent);
- begin
- Inherited Create (AOwner);
- fWindowHandle := AllocateHWnd (WndProc);
- fAniIcon := TAniIcon.Create;
- end;
-
- procedure TUCAniTrayIcon.Loaded;
- begin
- Inherited Loaded;
- UpdateTray (fVisible);
- end;
-
- destructor TUCAniTrayIcon.Destroy;
- begin
- SetAnimate (False);
- SetVisible (False);
- DeallocateHWnd (fWindowHandle);
- fAniIcon.Free;
- Inherited Destroy;
- end;
-
- procedure TUCAniTrayIcon.SetHint (const Value: String);
- begin
- if fHint <> Value then begin
- fHint := Value;
- UpdateTray (fVisible);
- end;
- end;
-
- procedure TUCAniTrayIcon.TrayMessage (var Message: TMessage);
- begin
- if not fExclusionLock then begin
- fExclusionLock := True;
- case Message.lParam of
- wm_LButtonDown: if Assigned (fOnLeftClick) then fOnLeftClick (Self);
- wm_RButtonDown: if Assigned (fOnRightClick) then fOnRightClick (Self);
- end;
- fExclusionLock := False;
- end;
- end;
-
- procedure TUCAniTrayIcon.WndProc (var Message: TMessage);
- begin
- with Message do if Msg = UCTrayCallBack then try
- TrayMessage (Message);
- except
- Application.HandleException (Self);
- end
- else if (Msg = wm_Timer) and (fAniIcon.Empty = False) and fVisible then begin
- fAniIcon.Animate;
- UpdateTray (fVisible);
- end
- else Result := DefWindowProc (fWindowHandle, Msg, wParam, lParam);
- end;
-
- procedure TUCAniTrayIcon.SetAniIcon (Value: TAniIcon);
- begin
- fAniIcon.Assign (Value);
- UpdateTray (fVisible);
- end;
-
- procedure TUCAniTrayIcon.SetVisible (Value: Boolean);
- begin
- if fVisible <> Value then UpdateTray (Value);
- fVisible := Value;
- end;
-
- procedure TUCAniTrayIcon.SetAnimate (Value: Boolean);
- begin
- if (fAnimate <> Value) then begin
- fAnimate := Value;
- if fAnimate then SetTimer (fWindowHandle, 1, 50, Nil)
- else KillTimer (fWindowHandle, 1);
- end;
- end;
-
- procedure TUCAniTrayIcon.UpdateTray (Visible: Boolean);
- var
- Msg: DWord;
- tid: TNotifyIconData;
- begin
- if not (csDesigning in ComponentState) then begin
- tid.cbSize := sizeof (tid);
- tid.Wnd := fWindowHandle;
- tid.uID := 0;
- tid.uFlags := nif_Message + nif_Icon + nif_Tip;
- tid.uCallbackMessage := UCTrayCallBack;
- if fAniIcon.Empty then tid.hIcon := Application.Icon.Handle
- else tid.hIcon := fAniIcon.Icon;
- StrPCopy (tid.szTip, fHint);
-
- if Visible <> fVisible then begin
- if Visible then Msg := nim_Add else Msg := nim_Delete;
- end else Msg := nim_Modify;
-
- Shell_NotifyIcon (Msg, @tid);
- end;
- end;
-
- procedure Register;
- begin
- RegisterComponents ('UnCommon', [TUCAniTrayIcon]);
- end;
-
- end.
-